home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / RDPASSWD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  4.3 KB  |  223 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
  3.  * $Author: jon $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  *
  11.  * This routine prints the supplied string to standard
  12.  * output as a prompt, and reads a password string without
  13.  * echoing.
  14.  */
  15.  
  16. #ifndef    lint
  17. static char rcsid_read_password_c[] =
  18. "$Header: read_password.c,v 4.12 89/05/30 18:01:30 jon Exp $";
  19. #endif    lint
  20.  
  21. #include <mit_copy.h>
  22. #include <des.h>
  23. #include "conf.h"
  24.  
  25. #include <stdio.h>
  26. #ifdef    BSDUNIX
  27. #include <strings.h>
  28. #include <sys/ioctl.h>
  29. #include <signal.h>
  30. #include <setjmp.h>
  31. #else
  32. char     *strcpy();
  33. int      strcmp();
  34. #ifndef index
  35. char     *index();
  36. #endif
  37. #endif
  38.  
  39. #ifdef    BSDUNIX
  40. static jmp_buf env;
  41. #endif
  42.  
  43. #ifdef BSDUNIX
  44. #ifdef POSIX
  45. typedef void sigtype;
  46. #else
  47. typedef int sigtype;
  48. #endif
  49. static sigtype sig_restore();
  50. static push_signals(), pop_signals();
  51. int des_read_pw_string();
  52. #endif
  53.  
  54. #ifdef IBMPC
  55. #include <conio.h>
  56. #endif
  57. /*** Routines ****************************************************** */
  58. int
  59. des_read_password(k,prompt,verify)
  60.     des_cblock *k;
  61.     char *prompt;
  62.     int    verify;
  63. {
  64.     int ok;
  65.     char key_string[BUFSIZ];
  66.  
  67. #ifdef BSDUNIX
  68.     if (setjmp(env)) {
  69.     ok = -1;
  70.     goto lose;
  71.     }
  72. #endif
  73.  
  74.     ok = des_read_pw_string(key_string, BUFSIZ, prompt, verify);
  75.     if (ok == 0)
  76.     des_string_to_key(key_string, k);
  77.  
  78. lose:
  79.     memset(key_string,0, sizeof (key_string));
  80.     return ok;
  81. }
  82.  
  83. #ifdef IBMPC
  84. static void gets_no_echo(char *s,int size)
  85. {
  86. #ifndef WINDOWS
  87.     char *end=s+size-1,*p=s;
  88.     while (s<end && (*s=getch()) != '\r')
  89.         s++;
  90.     *s='\0';
  91.     putch('\r');
  92.     putch('\n');
  93. #endif
  94. }
  95. #endif
  96.  
  97. /*
  98.  * This version just returns the string, doesn't map to key.
  99.  *
  100.  * Returns 0 on success, non-zero on failure.
  101.  */
  102.  
  103. int
  104. des_read_pw_string(s,max,prompt,verify)
  105.     char *s;
  106.     int    max;
  107.     char *prompt;
  108.     int    verify;
  109. {
  110.     int ok = 0;
  111.     char *ptr;
  112.     
  113. #ifdef BSDUNIX
  114.     jmp_buf old_env;
  115.     struct sgttyb tty_state;
  116. #endif
  117.     char key_string[BUFSIZ];
  118.  
  119.     if (max > BUFSIZ) {
  120.     return -1;
  121.     }
  122.  
  123. #ifdef    BSDUNIX
  124.     /* XXX assume jmp_buf is typedef'ed to an array */
  125.     bcopy((char *)old_env, (char *)env, sizeof(env));
  126.     if (setjmp(env))
  127.     goto lose;
  128.  
  129.     /* save terminal state*/
  130.     if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1) 
  131.     return -1;
  132.  
  133.     push_signals();
  134.     /* Turn off echo */
  135.     tty_state.sg_flags &= ~ECHO;
  136.     if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
  137.     return -1;
  138. #endif
  139.     while (!ok) {
  140.     (void) printf(prompt);
  141.     (void) fflush(stdout);
  142. #ifdef    IBMPC
  143.     gets_no_echo(s,max);
  144.     if (!strlen(s))
  145.         continue;
  146. #else
  147.     if (!fgets(s, max, stdin)) {
  148.         clearerr(stdin);
  149.         continue;
  150.     }
  151.     if ((ptr = index(s, '\n')))
  152.         *ptr = '\0';
  153. #endif
  154.     if (verify) {
  155.         printf("\nVerifying, please re-enter %s",prompt);
  156.         (void) fflush(stdout);
  157. #ifdef IBMPC
  158.         gets_no_echo(key_string,sizeof(key_string));
  159.         if (!strlen(key_string))
  160.         continue;
  161. #else
  162.         if (!fgets(key_string, sizeof(key_string), stdin)) {
  163.         clearerr(stdin);
  164.         continue;
  165.         }
  166.             if ((ptr = index(key_string, '\n')))
  167.         *ptr = '\0';
  168. #endif
  169.         if (strcmp(s,key_string)) {
  170.         printf("\n\07\07Mismatch - try again\n");
  171.         (void) fflush(stdout);
  172.         continue;
  173.         }
  174.     }
  175.     ok = 1;
  176.     }
  177.  
  178. #ifdef    BSDUNIX
  179. lose:
  180.     if (!ok)
  181.     bzero(s,0, max);
  182.     printf("\n");
  183.     /* turn echo back on */
  184.     tty_state.sg_flags |= ECHO;
  185.     if (ioctl(0,TIOCSETP,(char *)&tty_state))
  186.     ok = 0;
  187.     pop_signals();
  188.     bcopy((char *)env, (char *)old_env, sizeof(env));
  189. #endif
  190.     if (verify)
  191.     memset(key_string, 0,sizeof (key_string));
  192.     s[max-1] = 0;        /* force termination */
  193.     return !ok;            /* return nonzero if not okay */
  194. }
  195.  
  196. #ifdef    BSDUNIX
  197. /*
  198.  * this can be static since we should never have more than
  199.  * one set saved....
  200.  */
  201. static sigtype (*old_sigfunc[NSIG])();
  202.  
  203. static push_signals()
  204. {
  205.     register i;
  206.     for (i = 0; i < NSIG; i++)
  207.     old_sigfunc[i] = signal(i,sig_restore);
  208. }
  209.  
  210. static pop_signals()
  211. {
  212.     register i;
  213.     for (i = 0; i < NSIG; i++)
  214.     (void) signal(i,old_sigfunc[i]);
  215. }
  216.  
  217. static sigtype
  218. sig_restore()
  219. {
  220.     longjmp(env,1);
  221. }
  222. #endif
  223.